package com.common.library.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    public Activity mContext;
    private View inflate;
    private Unbinder mUnBinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getActivity();
        if (inflate == null) {
            inflate = LayoutInflater.from(mContext).inflate(getLayoutId(), null);
        }
        ViewGroup parent = (ViewGroup) inflate.getParent();
        if (parent != null) {
            parent.removeView(inflate);
        }
        mUnBinder = ButterKnife.bind(this,inflate);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initVar();
        initData();
        initListener();
    }

    protected abstract void initVar();

    public abstract int getLayoutId();

    public abstract void initData();

    public abstract void initListener();


    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnBinder.unbind();
    }
}
